package com.yingxin.yxerp.module.report.task.request.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.enums.DynamicTaskSetTypeEnums;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yxerp.module.report.service.IDynamicTaskSetService;
import com.yingxin.yxerp.module.report.task.request.constant.InterfaceUrl;
import com.yingxin.yxerp.module.report.task.request.dto.ResultKeyValueDto;
import com.yingxin.yxerp.module.report.task.request.dto.ResultListDto;
import com.yingxin.yxerp.module.report.task.request.dto.ResultOneDto;
import com.yingxin.yxerp.module.report.task.request.dto.top.Dto;
import com.yingxin.yxerp.module.report.task.request.service.InterfaceService;
import com.yingxin.yxerp.module.report.task.request.vo.req.DynamicTaskRequestQueryVo;
import com.yingxin.yxerp.module.report.task.request.vo.req.DynamicTaskRequestUpdateVo;
import com.yingxin.yxerp.module.report.task.request.vo.resp.DynamicTaskRequestRespVo;
import com.yingxin.yxerp.module.report.task.request.vo.resp.KeyValueRequestRespVo;
import com.yingxin.yxerp.module.report.vo.resp.DynamicTaskSetRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 请求接口 - 实现类
 * @author yuebao
 * @date 2023-12-08
 */

@Service
public class InterfaceServiceImpl implements InterfaceService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IDynamicTaskSetService dynamicTaskSetService;

    /**
     * 获取 url 地址
     */
    private String getURL(String part_url){
        DynamicTaskSetRespVo vo = dynamicTaskSetService.mapper().getOneVo(
                DynamicTaskSetTypeEnums.TASK_URL.getType() );
        return vo.getTaskUrl() + part_url;
    }

    private HttpHeaders setRestTemplate(){
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = new MediaType(
                "application", "json", Charset.forName("UTF-8") );
        headers.setContentType( mediaType );
        headers.setAccept( Collections.singletonList( MediaType.APPLICATION_JSON_UTF8 ) );

        return headers;
    }

    /**
     * 查询全部任务列表
     */
    @Override
    public List<DynamicTaskRequestRespVo> list( DynamicTaskRequestQueryVo queryVo ){
        try {
            String json = JSON.toJSONString( queryVo );

            HttpHeaders headers = setRestTemplate();

            HttpEntity<String> request = new HttpEntity<>( json, headers );

            ResponseEntity<String> response = restTemplate.postForEntity(
                    getURL( InterfaceUrl.GET_LIST ), request, String.class );

            ResultListDto dto = JSON.parseObject( response.getBody(), new TypeReference<ResultListDto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return Arrays.asList( dto.getData() );
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 根据id查询 {id}
     */
    @Override
    public DynamicTaskRequestRespVo getVo( String id ){
        try {
            ResponseEntity<String> response = restTemplate.getForEntity(
                    getURL( InterfaceUrl.GET_BY_ID ) + id, String.class );
            ResultOneDto dto = JSON.parseObject( response.getBody(), new TypeReference<ResultOneDto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return dto.getData();
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 修改
     */
    @Override
    public Dto update(DynamicTaskRequestUpdateVo updateVo ){
        try {
            String json = JSON.toJSONString( updateVo );

            HttpHeaders headers = setRestTemplate();

            HttpEntity<String> request = new HttpEntity<>( json, headers );

            ResponseEntity<String> response = restTemplate.postForEntity(
                    getURL( InterfaceUrl.UPDATE ), request, String.class );

            Dto dto = JSON.parseObject( response.getBody(), new TypeReference<Dto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return dto;
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 任务执行一次 {id}
     */
    @Override
    public Dto run( String id ){
        try {
            ResponseEntity<String> response = restTemplate.getForEntity(
                    getURL( InterfaceUrl.RUN ) + id, String.class );
            Dto dto = JSON.parseObject( response.getBody(), new TypeReference<Dto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return dto;
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 任务启动/停止
     */
    @Override
    public Dto StartOrStop( String id, boolean status ){
        try {
            DynamicTaskRequestUpdateVo updateVo = new DynamicTaskRequestUpdateVo();
            updateVo.setId( id );
            updateVo.setStatus( status );

            String json = JSON.toJSONString( updateVo );

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType( MediaType.APPLICATION_JSON );

            HttpEntity<String> request = new HttpEntity<>( json, headers );

            ResponseEntity<String> response = restTemplate.postForEntity(
                    getURL( InterfaceUrl.UPDATE_STATUS ), request, String.class );
            Dto dto = JSON.parseObject( response.getBody(), new TypeReference<Dto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return dto;
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 任务删除 {id}
     */
    @Override
    public Dto delete( String id ){
        try {
            ResponseEntity<String> response = restTemplate.getForEntity(
                    getURL( InterfaceUrl.DELETE_BY_ID ) + id, String.class );
            Dto dto = JSON.parseObject( response.getBody(), new TypeReference<Dto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return dto;
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 联机 {id}
     */
    @Override
    public Dto online( String id ){
        try {
            ResponseEntity<String> response = restTemplate.getForEntity(
                    getURL( InterfaceUrl.ONLINE ) + id, String.class );
            Dto dto = JSON.parseObject( response.getBody(), new TypeReference<Dto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return dto;
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 来源 数据集
     */
    @Override
    public List<KeyValueRequestRespVo> getCategoryList(){
        try {
            ResponseEntity<String> response = restTemplate.postForEntity(
                    getURL( InterfaceUrl.CATEGORY_LIST ),null, String.class );

            ResultKeyValueDto dto = JSON.parseObject(
                    response.getBody(), new TypeReference<ResultKeyValueDto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return Arrays.asList( dto.getData() );
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 任务分类 数据集
     */
    @Override
    public List<KeyValueRequestRespVo> getTaskTypeList(){
        try {
            ResponseEntity<String> response = restTemplate.postForEntity(
                    getURL( InterfaceUrl.TASK_TYPE_LIST ),null, String.class );

            ResultKeyValueDto dto = JSON.parseObject(
                    response.getBody(), new TypeReference<ResultKeyValueDto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return Arrays.asList( dto.getData() );
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 任务名称 数据集
     */
    @Override
    public List<KeyValueRequestRespVo> getTaskNameList(){
        try {
            ResponseEntity<String> response = restTemplate.postForEntity(
                    getURL( InterfaceUrl.TASK_NAME_LIST ),null, String.class );

            ResultKeyValueDto dto = JSON.parseObject(
                    response.getBody(), new TypeReference<ResultKeyValueDto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return Arrays.asList( dto.getData() );
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

    /**
     * 获取任务执行记录状态 数据集
     */
    @Override
    public List<KeyValueRequestRespVo> getStatusList(){
        try {
            ResponseEntity<String> response = restTemplate.postForEntity(
                    getURL( InterfaceUrl.STATUS_LIST ),null, String.class );

            ResultKeyValueDto dto = JSON.parseObject(
                    response.getBody(), new TypeReference<ResultKeyValueDto>(){} );

            if( dto.getCode().trim().equals( CommonConst.FALSE ) ){
                return Arrays.asList( dto.getData() );
            }else{
                throw new BizException( dto.getMsg() );
            }
        }catch (Exception e){
            throw new BizException( e.getMessage() );
        }
    }

}
